package cn.tedu.jxc.controller;

import cn.tedu.jxc.aop.LogAnnotation;
import cn.tedu.jxc.mapper.RoleMapper;
import cn.tedu.jxc.pojo.dto.RoleDTO;
import cn.tedu.jxc.pojo.dto.RoleUpdateDTO;
import cn.tedu.jxc.pojo.vo.DataVO;
import cn.tedu.jxc.pojo.vo.RoleOptionsVO;
import cn.tedu.jxc.pojo.vo.RoleSelectByRoleNameVO;
import cn.tedu.jxc.pojo.vo.RoleSelectVO;
import cn.tedu.jxc.security.LoginPrincipal;
import cn.tedu.jxc.service.RoleService;
import cn.tedu.jxc.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("roles")
@Api(tags = "02.角色管理模块")
public class RoleController {

    @Autowired
    RoleService roleService;

    @Autowired(required = false)
    RoleMapper mapper;

    @RequestMapping("/select")
    public List<RoleSelectVO> findAllRole(){
        return mapper.selects();
    }

    @RequestMapping("/selectCount")
    public int count(){
        return mapper.selectCount();
    }

    @ApiOperation("根据id删除角色")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[\\d]+}/deleteById")
    @LogAnnotation(module = 1,operator = "删除角色")
    public JsonResult<Void> deleteById(@Range(min = 1) @PathVariable Integer id){
        roleService.deleteById(id);
        return JsonResult.ok();
    }
    @ApiOperation("添加角色")
    @ApiOperationSupport(order = 300)
    @RequestMapping("/add-new")
    @LogAnnotation(module = 1,operator = "添加角色")
    public JsonResult<Void> insert(RoleDTO role){
        roleService.addNew(role);
        log.debug("添加角色成功!");
        return JsonResult.ok();
    }


    @GetMapping("/selectById")
    public JsonResult<RoleSelectVO> selectById(Integer id){
        RoleSelectVO updateInfo = roleService.getUpdateInfo(id);
        return JsonResult.ok(updateInfo);
    }


    @ApiOperation("修改角色")
    @ApiOperationSupport(order = 310)
    @PostMapping("/update")
    @LogAnnotation(module = 1,operator = "修改角色")
    public JsonResult<Void> update(RoleUpdateDTO roleUpdateDTO){
        roleService.update(roleUpdateDTO);
        log.debug("修改成功!");
        return JsonResult.ok();
    }

    @RequestMapping("/selectByRole")
    public List<RoleSelectByRoleNameVO> selectByUsername(String name){
        return mapper.selectByRoleName(name);
    }

    @RequestMapping("/option")
    public List<RoleOptionsVO> roleOptions(){
        return mapper.selectOptions();
    }

    @RequestMapping("/selectBySelectId")
    public String selectBySelectId(int id){
        return mapper.selectBySelectId(id);
    }

    @RequestMapping("/getCount")
    public int getCount(String name){
        return mapper.selectCountByName(name);
    }

    @GetMapping("/selectRoleName")
    public JsonResult<List<String>> getRoleName(){
        List<String> roleName = roleService.getRoleName();
        return JsonResult.ok(roleName);
    }

    @GetMapping("/selectCount")
    public JsonResult<Integer[]> getCountOrderByRole(){
        Integer[] countOrderByRole = roleService.getCountOrderByRole();
        return JsonResult.ok(countOrderByRole);
    }
    @GetMapping("/selectData")
    public JsonResult<List<DataVO>> getData(){
        List<DataVO> data = mapper.getData();
        log.debug("date:{}",data);
        return JsonResult.ok(data);
    }
}
